import React from 'react';

export class Parent extends React.Component {
  constructor() {
    super();
    this.state = {
      msg: '',
    };
  }

  // 父组件定义传消息的函数
  sendMsg = (msg) => {
    console.log(msg);
    this.setState({
      msg,
    });
  };

  render() {
    return (
      <div style={{ border: '1px solid red', padding: 20 }}>
        <h2>父组件</h2>
        <h2>子组件说：{this.state.msg}</h2>
        <Child sendMsg={this.sendMsg}></Child>
      </div>
    );
  }
}

class Child extends React.Component {
  render() {
    return (
      <div style={{ border: '1px solid blue', padding: 20 }}>
        <h2>子组件</h2>
        <button onClick={() => this.props.sendMsg('没钱了')}>有需求</button>
      </div>
    );
  }
}
